% File_1 = 'c:\manoj\ocean\x3dg\tsu_results\res2\tsu1.res';
% fid = fopen(File_1, 'r', 'n');
% LBlock = fread(fid, 1, 'long');
% NO = fread(fid, 1, 'long');
% Period = fread(fid, 1, 'double'); 
% np = fread(fid, 1, 'long');
% nt = fread(fid, 1, 'long');
% fclose(fid);


for i = 1:1,
    
fid = fopen(['E:\manoj\ocean\tsu_results\tsu' sprintf('%d',i) '.res'],'rb');
fprintf('%s\n',['E:\manoj\ocean\tsu_results\tsu' sprintf('%d',i) '.res']);
fread(fid,518400,'uchar'); %Header includes the gird size, files used for computation etc
[Hpr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi; %in nT
[Hpi(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Htr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Hti(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Hrr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Hri(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Epr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*1.E+06; %in mV/km
[Epi(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*1.E+06;
[Etr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*1.E+06;
[Eti(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*1.E+06;
[Err(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*1.E+06;
[Eri(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*1.E+06;
fclose(fid);

% subplot(211);
% imagesc(Htr);
% axis([40,120,60,120]);
% caxis([-0.05 0.05]);
% colorbar;
% subplot(212);
% imagesc(Etr);
% axis([40,120,60,120]);
% caxis([-0.05 0.05]);
% colorbar;
j = sqrt(-1);
Hp(i,:,:) = Hpr(60:120,40:120)+j*Hpi(60:120,40:120);
Ht(i,:,:) = Htr(60:120,40:120)+j*Hti(60:120,40:120);
Hr(i,:,:) = Hrr(60:120,40:120)+j*Hri(60:120,40:120);
Ep(i,:,:) = Epr(60:120,40:120)+j*Epi(60:120,40:120);
Et(i,:,:) = Etr(60:120,40:120)+j*Eti(60:120,40:120);
Er(i,:,:) = Err(60:120,40:120)+j*Eri(60:120,40:120);

% worldmap([-30,30],[40,120]);
% h=contourfm(flipud(AA),[1,30,40]);
% pause(1);
end;

s = dir('E:\manoj\ocean\tsu\*.upw');

for i = 1:100,
    
fid = fopen(['G:\tsu_res\\' s(i).name],'rb');
fprintf('%s\n',s(i).name);
fread(fid,518400,'uchar'); %Header includes the gird size, files used for computation etc
[Hpr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi; %in nT
[Hpi(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Htr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Hti(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Hrr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Hri(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
fclose(fid);

j = sqrt(-1);
Hp(i,:,:) = Hpr(60:120,40:120)+j*Hpi(60:120,40:120);
Ht(i,:,:) = Htr(60:120,40:120)+j*Hti(60:120,40:120);
Hr(i,:,:) = Hrr(60:120,40:120)+j*Hri(60:120,40:120);

end;


fid = fopen('E:\manoj\ocean\cable_results\x3dg\apr00.res','rb');
fread(fid,518400,'uchar'); %Header includes the gird size, files used for computation etc
[Hpr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi; %in nT
[Hpi(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Htr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Hti(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Hrr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Hri(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Epr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*1.E+06; %in mV/km
[Epi(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*1.E+06;
[Etr(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*1.E+06;
[Eti(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*1.E+06;
[Err(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*1.E+06;
[Eri(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*1.E+06;
fclose(fid);


% To open a single UPW file

fid = fopen('E:\manoj\ocean\cable_results\x3dg\apr000.upw','rb');
fread(fid,518400,'uchar'); %Header includes the gird size, files used for computation etc
[Hpr1(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi; %in nT
[Hpi1(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Htr1(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Hti1(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Hrr1(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
[Hri1(:,:)] = reshape(fread(fid,360*180,'float32'),[180,360])*400*pi;
fclose(fid);

j = sqrt(-1);
Hp1(:,:) = Hpr(60:120,40:120)+j*Hpi(60:120,40:120);
Ht1(:,:) = Htr(60:120,40:120)+j*Hti(60:120,40:120);
Hr1(:,:) = Hrr(60:120,40:120)+j*Hri(60:120,40:120);

end;

